import time
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager

""" 窗口切换
# 1. 打开浏览器驱动管理
ChromeIns = ChromeDriverManager().install()
# 2. 创建谷歌浏览器驱动对象
driver = webdriver.Chrome(service=Service(ChromeIns))

# 3. 输入百度网址 + 获取当前窗口标题和URL
driver.get('https://www.baidu.com')
before_title = driver.title
print(f'before: {before_title}')
before_url = driver.current_url
print(f'before: {before_url}')

# 4. 查找元素 + 点击百度首页图片按钮 生成新窗口
driver.find_element(By.CSS_SELECTOR, '#s-top-left > a:nth-child(6)').click()
time.sleep(1)

# 5. 获取当前窗口句柄 + 获取所有窗口 + 切换到新窗口
cur_window = driver.current_window_handle
all_window = driver.window_handles
for window in all_window:
    if cur_window != window:
        # driver.switch_to_window() 老写法无法使用
        driver.close()  # 关闭旧的窗口
        driver.switch_to.window(window) # 切换为新窗口
        break
time.sleep(2)

# 6. 获取新窗口的标题和URL
after_title = driver.title
print(f'after: {after_title}')
after_url = driver.current_url
print(f'after: {after_url}')

# 7. 关闭浏览器
driver.quit()
"""

""" 窗口大小切换
# 1. 打开浏览器驱动管理
ChromeIns = ChromeDriverManager().install()
# 2. 创建谷歌浏览器驱动对象 + 输入百度网址
driver = webdriver.Chrome(service=Service(ChromeIns))
driver.get('https://www.baidu.com')
# 3. 最大化窗口
driver.maximize_window()
time.sleep(2)
# 4. 最小化窗口
driver.minimize_window()
time.sleep(2)
# 5. 自定义大小窗口
driver.set_window_size(1024,512)
time.sleep(2)
# 6. 关闭浏览器
driver.quit()
"""
